from appium.webdriver.webdriver import WebDriver

class DebugTools:
    def __init__(self, driver: WebDriver):
        self.driver = driver

    def print_contexts(self):
        """
        打印当前所有上下文
        """
        try:
            contexts = self.driver.contexts
            print(f"[DEBUG] 当前上下文: {contexts}")
            print(f"[DEBUG] 当前所在上下文: {self.driver.current_context}")
        except Exception as e:
            print(f"[ERROR] 获取 contexts 失败: {e}")

    def print_page_source(self, length=500):
        """
        打印当前页面结构（默认只截取前500字符，防止太长）
        """
        try:
            source = self.driver.page_source
            print(f"[DEBUG] 当前页面结构(截取): {source[:length]} ...")
        except Exception as e:
            print(f"[ERROR] 获取 page_source 失败: {e}")
